home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / c / RConfig.lha / RConfig_v1.1 / docs / stkchk.doc < prev   
Encoding:
Text File  |  1992-09-13  |  5.3 KB  |  130 lines

  1. TYPE
  2.     _stkchk(R),_stkfree(R) - Miscellaneous
  3.  
  4. NAME
  5.     _stkchk - check for stack overflow
  6.     _stkfree - unwind dynamic stack
  7.  
  8. SYNOPSIS
  9.     #pragma regcall(_stkchk(d0))
  10.     void _stkchk(signed short frame_size)
  11.  
  12. DESCRIPTION
  13.     This module contains two functions:
  14.  
  15.         void stkchk ( stack_frame_size : register d0 )
  16.         void stkfree ()
  17.  
  18.     The function stkchk() is called by code generated by the compiler
  19.     (when compiling with the -bd option).  The function stkfree() is
  20.     private, and should never be called directly by user programs.
  21.  
  22.     Advantages
  23.     ~~~~~~~~~~
  24.         o   Checks stack frame requirements (for local variables)--usually
  25.             'link'ed--before overflowing the stack (where a call to stkchk
  26.             would have caused non-stack memory to be munged).
  27.  
  28.         o   Dynamic stack sizing (if enabled).  Allows stack hungry programs
  29.             to continue running, until the program exits normally (or you
  30.             run out of memory).
  31.  
  32.         o   Compatible with RConfig's setjmp(),longjmp(), which don't
  33.             bypass stkfree().
  34.  
  35.     Disadvantages
  36.     ~~~~~~~~~~~~~
  37.         o   What disadvantages?!
  38.  
  39. CONSTANTS
  40.     MIN_STACK is the minimum amount of stack space that should be available
  41.     and is currently set at 2768.  This is not entirely arbitrary.  The
  42.     theoretical minimum is 72 bytes (saving registers for a context switch).
  43.     However, a call to printf() can require 1K of stack [LIBSRC52]; processes
  44.     that call dos.library need an additional 1500 bytes of stack [RKMLIB90];
  45.     and we also use 4 bytes for a magic cookie to test if the stack has
  46.     somehow overflowed between calls--hopefully, the extra 192 bytes provides
  47.     a safe margin.  Feel free to increase this for your application.
  48.  
  49.     STACK_SIZE is the amount of additional stack space that should be
  50.     allocated (if the existing stack will overflow).  Currently set to 8192,
  51.     this value should be larger than MIN_STACK.
  52.  
  53.     CONTEXT_SIZE is the amount of stack info that should be copied from a
  54.     stack to the extension stack.  This is due to stack buffers not being
  55.     linked contiguously.  At minimum, this is the largest amount pushed
  56.     onto the stack for a procedure call.  Obviously, the larger the context,
  57.     the longer it takes to copy it.
  58.  
  59.     Note: These values should all be even numbers (evenly divisible by 2),
  60.           to keep the stack "word aligned".
  61.  
  62.  
  63. SOURCE COMMENTS
  64.     1)  __stkchk:
  65.                 ...
  66.           lea    CONTEXT_SIZE+8(a7),a1
  67.                 ...
  68.           pea    __stkfree#
  69.  
  70.         The '8' is the offset to the context on the stack.  We skip the
  71.         return address (that stkchk will return to); we also skip the return
  72.         address that the calling procedure will return to when it ends, and
  73.         replace it with the address to stkfree.  So, when the calling
  74.         procedure ends (and unwinds the stack), stkfree is called to free
  75.         the extension stack buffer.
  76.  
  77.     2)    move.l  __savsp,a1
  78.                 ...
  79.           bra.s   copynextword
  80.  
  81.         The main purpose of the code is this section is to avoid reading
  82.         (too far) beyond a program's stack.  In addition, this has the
  83.         benefit of avoiding Enforcer hits in the case where a program's
  84.         stack is located at the upper boundary of physical memory, reading
  85.         beyond the bottom of the stack would be accessing nonexistent memory.
  86.  
  87.     3)  __stkfree:
  88.                 ...
  89.           jsr     __free
  90.           addq.l  #8,a7
  91.  
  92.         This pops the pointer (to the empty stack) used to setup the free()
  93.         call.  Also, pop the original return address when stkchk was called
  94.         that allocated this stack buffer--stkchk returned using this address
  95.         on the extension stack buffer.
  96.  
  97. ERRORS
  98.     Q)  My program crashes even with DynaStack.
  99.     A)  And that's my fault?  :-)
  100.  
  101.         Try increasing MIN_STACK, CONTEXT_SIZE, or your CLI's STACK.  Make
  102.         sure you have DYNASTACK defined when you re-assemble.  Also, don't
  103.         fiddle with the stack contents, beyond what would normally be visible
  104.         (in scope) to your procedure.
  105.  
  106.     Q)  I ported <some gnu_program_name> and it doesn't work with DynaStack.
  107.     A)  It's likely that it uses alloca() [in alloca.c], which allocates
  108.         temporary storage based on the stack pointer.  Because DynaStack
  109.         buffers aren't continguous (or ordered in memory), GNU alloca() may
  110.         be freeing memory that it shouldn't...yet.  Try recompiling it
  111.         without DynaStack.
  112.  
  113.         [ Personally, I'd recommend that you scrap GNU alloca() altogether,
  114.           as memory may not be freed in a timely manner--the stack pointer
  115.           isn't always the same upon entry to C functions at the same
  116.           activation level, varying with the number of parameters pushed
  117.           on to the stack and the size of the stack frame for local
  118.           variables.  If you really want an alloca() function, I have one
  119.           available...and it works with DynaStack. ]
  120.  
  121.  
  122. REFERENCES
  123.     LIBSRC52 - Manx Software Systems, Inc.  Files: "stdio/format.c",
  124.                "stdio/_format.c", and "sysio/stkchk.a68" from Manx's
  125.                Aztec C Library Source (5.2a distribution for the Amiga),
  126.                November 1991.
  127.  
  128.     RKMLIB90 - Commodore-Amiga, Inc.  Amiga ROM Kernel Reference Manual:
  129.                Libraries & Devices (for V1.3), p 268, 1990.
  130.